package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.pojo.Infocount;
import com.example.visitedsys.redis.RedisUtil;
import com.example.visitedsys.service.InfocountService;
import com.example.visitedsys.service.VisitorService;
import com.wuwenze.poi.ExcelKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈统计访客〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/21 15:34
 * @version 1.0
 */

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    RedisUtil redisutil;
    public String export_key = "report2";
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","统计访客模块");
    }
    private static final Logger logger = LogManager.getLogger(StatisticsController.class);
    @Autowired
    private VisitorService visitorService;
    @Autowired
    private InfocountService infocountService;
    @RequiresPermissions("statistics:select")
    @GetMapping("/getAll")
    public ResponseBean statistics(@RequestParam("startTime") Date startTime, @RequestParam("endTime") Date endTime){
        logger.info("统计访客");
        if(redisutil.hasKey(export_key)){
            return ResponseBean.success(redisutil.get(export_key));
        }
        else{
            ArrayList arrayList =visitorService.statistics(startTime,endTime);
            redisutil.set(export_key,arrayList,600);
            return ResponseBean.success(arrayList);
        }

    }
    /**
     * 导出统计访客信息excel
     * @param response
     */
    @PostMapping("/export")
    @RequiresPermissions("statistics:export")
    public void export(HttpServletResponse response) {
        logger.info("导出统计访客信息表格");
        List<Infocount> infocounts = this.infocountService.findAll();
        ExcelKit.$Export(Infocount.class, response).downXlsx(infocounts, false);
    }

}
